<template>
  <div id="modesTemplateIndex">
    <el-row>
      <!-- 表格查询条件 -->
      <el-col :span="24">
        <el-form :inline="true" ref="stationForm" :model="queryForm">
          <el-form-item>
            <el-input v-model="queryForm.code" placeholder="激活码模板编号/名称"></el-input>
          </el-form-item>
          <el-form-item>
            <gd_param type="select"
                      paramCode="gptVipType"
                      placeholder="会员类型"
                      v-model="queryForm.gptVipTypeCode" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="getTableDatas()">查询</el-button>
            <el-button @click="resetQueryForm" icon="el-icon-refresh-right">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-row>
      <!-- 数据表格 -->
      <el-col :span="24">
        <el-row>
          <el-col :span="24">
            <el-button type="primary" icon="el-icon-plus" @click="dialog.saveOrEditDiv=true;getAIModesList()">新增模型</el-button>
            <el-popconfirm title="这是一段内容确定删除吗？" @confirm="moreDeletes">
              <el-button slot="reference" type="danger" icon="el-icon-delete">批量删除模型</el-button>
            </el-popconfirm>
          </el-col>
        </el-row>
        <el-row style="margin-top:10px;">
          <el-col :span="24">
            <el-table :data="tableData" border style="width: 100%" @selection-change="tableDataSelected">
              <el-table-column type="selection" width="55"></el-table-column>
              <el-table-column prop="code" label="编号"></el-table-column>
              <el-table-column prop="name" label="名称"></el-table-column>
              <el-table-column prop="gptVipTypeName" label="会员类型">
                <template slot-scope="scope">
                  <el-tag :type="getTagType(scope.row.gptVipTypeCode)">{{scope.row.gptVipTypeName}}</el-tag>
                </template>
              </el-table-column>
              <el-table-column prop="isNewStaffTemplate" label="是否新用户注册未激活模板">
                <template slot-scope="scope">
                  <el-switch
                    v-model="scope.row.isNewStaffTemplate"
                    active-value="1"
                    inactive-value="0"
                    @change="isNewStaffTemplateChange(scope.row.isNewStaffTemplate,scope.row)"
                  >
                  </el-switch>
                </template>
              </el-table-column>
              <el-table-column fixed="right" label="操作" align="center" width="180">
                <template slot-scope="scope">
                  <el-button @click="rowShow(scope.row)" size="small" icon="el-icon-search" circle title="查看"></el-button>
                  <el-button @click="rowEdit(scope.row)" type="primary" size="small" icon="el-icon-edit" circle title="编辑"></el-button>
                  <el-button @click="rowDelete(scope.row)" type="danger" icon="el-icon-delete" size="small" circle title="删除"></el-button>
                  <el-dropdown>
                    <span class="el-dropdown-link"><i class="el-icon-arrow-down el-icon--right"></i></span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item><span @click="rowMoreCreate(scope.row)">批量开卡</span></el-dropdown-item>
                      <el-dropdown-item><span @click="rowShowActiveCode(scope.row)">查看激活卡</span></el-dropdown-item>
                      <el-dropdown-item><span @click="rowShowActiveCodeCanUse(scope.row)">复制可用卡</span></el-dropdown-item>
                      <el-dropdown-item><span @click="rowShowActiveCodeCanUseEx(scope.row)">导出可用卡</span></el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              @size-change="tableSizeChange"
              @current-change="tableCurrentChange"
              :current-page="pageData.page"
              :page-sizes="[10, 20, 40, 100]"
              :page-size="pageData.size"
              layout="total, sizes, prev, pager, next, jumper"
              :total="pageData.total"
            ></el-pagination>
          </el-col>
        </el-row>
      </el-col>
    </el-row>

    <!-- 新增\修改用户信息弹窗 -->
    <el-dialog
      :title="(dialog.isEdit == true ? '编辑' : '新增') + '模板'"
      :visible.sync="dialog.saveOrEditDiv"
      :close-on-click-modal="false"
      :show-close="false"
      width="80%"
      top="20px"
      :destroy-on-close="true"
    >
      <el-form
        ref="saveOrEditDataForm"
        :rules="saveOrEditDataFormRules"
        :model="saveOrEditData.template"
        label-width="110px"
      >
        <el-form-item label="模板名称" prop="name">
          <el-input
            v-model="saveOrEditData.template.name"
            placeholder="请填写模板名称"
            maxlength="80"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item label="会员类型" prop="gptVipTypeCode">
          <gd_param type="select"
                    paramCode="gptVipType"
                    placeholder="会员类型"
                    @selectChange="gptVipTypeChange"
                    v-model="saveOrEditData.template.gptVipTypeCode" />
        </el-form-item>
        <el-form-item label="备注" prop="comment">
          <el-input
            type="textarea"
            placeholder="如有备注请输入描述"
            v-model="saveOrEditData.template.comment"
            maxlength="100"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-divider content-position="left">AI模型定价列表</el-divider>
        <el-row v-if="dialog.isEdit==true" style="display: flex;align-items: center;margin-bottom: 10px">
          <el-col :span="4">将此模型添加到模板中：</el-col>
          <el-col :span="12">
            <el-select v-model="editNowSelectModel" filterable clearable  placeholder="请选择要添加的模型">
              <el-option
                v-for="item in nowModelList"
                :key="item.modelsID"
                :label="item.modelsName"
                :value="item.modelsID">
              </el-option>
            </el-select>
            <el-button type="primary" @click="addModelForEdit">添加</el-button>
          </el-col>
        </el-row>
        <el-table :data="saveOrEditData.infos" border style="width: 100%">
          <el-table-column prop="modelsName" label="模型名称"></el-table-column>
          <el-table-column prop="modeGrantUseTypeCode" label="授权类别">
            <template slot-scope="scope">
              <el-select v-model="scope.row.modeGrantUseTypeCode" placeholder="请选择授权类别">
                <el-option
                  v-for="item in paramsData.modeGrantUseType"
                  :key="item.dataValue"
                  :label="item.dataName"
                  :value="item.dataValue">
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="isHaveDayLimit" label="是否有每日上限" width="80">
            <template slot-scope="scope">
              <el-switch
                @change="rowIsHaveDayLimitChange(scope.row)"
                v-model="scope.row.isHaveDayLimit"
                active-value="1"
                inactive-value="0">
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="dayLimitCount" label="每日上限设置(“-1”代表无上限)">
            <template slot-scope="scope">
              <el-slider
                :disabled="scope.row.isHaveDayLimit!=1?true:false"
                :min="-1"
                :max="999"
                v-model="scope.row.dayLimitCount"
                show-input>
              </el-slider>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" align="center" width="80">
            <template slot-scope="scope">
              <el-button @click="rowInfoDelete(scope.row)" type="danger" icon="el-icon-delete" size="small" circle title="删除"></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 按钮组 -->
        <div style="margin-top: 10px">
          <el-button type="primary" @click="submitSaveOrEdit" :loading="formLoading">确定</el-button>
          <el-button
            @click="
							dialog.saveOrEditDiv = false;
              dialog.isEdit = false;
							resetAddOrEditOrShowForm();
						"
          >
            取消
          </el-button>
        </div>
      </el-form>
    </el-dialog>

    <!-- 详情弹窗 -->
    <el-dialog
      title="模型详情"
      :visible.sync="dialog.showDiv"
      width="85%"
      :show-close="false"
      :destroy-on-close="true"
      :close-on-click-modal="false"
    >
      <el-descriptions class="margin-top" :column="3" border>
        <el-descriptions-item label="名称">{{ showData.template.name }}</el-descriptions-item>
        <el-descriptions-item label="编号">{{ showData.template.code }}</el-descriptions-item>
        <el-descriptions-item label="会员类型">{{ showData.template.gptVipTypeName }}</el-descriptions-item>
        <el-descriptions-item label="是否新用户注册未激活模板">{{ showData.template.isNewStaffTemplate==0?'否':'是' }}</el-descriptions-item>
        <el-descriptions-item label="备注">{{ showData.template.comment }}</el-descriptions-item>
      </el-descriptions>
      <el-divider content-position="left">AI模型定价列表</el-divider>
      <el-table :data="showData.infos" border style="width: 100%">
        <el-table-column prop="modelsName" label="模型名称"></el-table-column>
        <el-table-column prop="modeGrantUseTypeCode" label="授权类别">
          <template slot-scope="scope">
            <el-tag :type="getTagType(scope.row.modeGrantUseTypeCode)">{{scope.row.modeGrantUseTypeName}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="isHaveDayLimit" label="是否有每日上限">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.isHaveDayLimit==0" type="success">无上限</el-tag>
            <el-tag v-else type="warning">每日上限：{{ scope.row.dayLimitCount }}条</el-tag>
          </template>
        </el-table-column>
      </el-table>
      <el-button
        @click="
							dialog.showDiv = false;
							resetAddOrEditOrShowForm();
						"
      >
        关闭
      </el-button>
    </el-dialog>

    <!-- 批量开卡 -->
    <el-dialog
      :title="'根据模板批量开卡-'+moreCreateData.templateName"
      :visible.sync="dialog.moreCreate"
      width="60%"
      :show-close="false"
      :destroy-on-close="true"
      :close-on-click-modal="false"
    >
      <el-form
        ref="moreCreateDataForm"
        :rules="moreCreateDataFormRules"
        :model="moreCreateData"
        label-width="110px"
      >
        <el-form-item label="激活卡类型" prop="activeCodeTypeCode">
          <gd_param type="select"
                    paramCode="activeCodeType"
                    placeholder="请选择激活卡类型"
                    @selectChange="activeCodeTypeChange"
                    v-model="moreCreateData.activeCodeTypeCode" />
        </el-form-item>
        <el-form-item label="激活天数" prop="activeDays">
          <el-input
            type="number"
            v-model="moreCreateData.activeDays"
            placeholder="请填写激活天数"
            maxlength="10"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item label="开卡数量" prop="createCount">
          <el-input
            type="number"
            v-model="moreCreateData.createCount"
            placeholder="请填写开卡数量"
            maxlength="10"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="comment">
          <el-input
            type="textarea"
            placeholder="如有备注请输入描述"
            v-model="moreCreateData.comment"
            maxlength="100"
            show-word-limit
          ></el-input>
        </el-form-item>
        <div style="margin-top: 10px">
          <el-button type="primary" @click="submitMoreCreate" :loading="formLoading">确定</el-button>
          <el-button @click="closeMoreCreate" >取消</el-button>
        </div>
      </el-form>
    </el-dialog>

    <!-- 模板开卡详情 -->
    <el-dialog
      title="模板开卡详情"
      :visible.sync="dialog.activeCodes"
      width="85%"
      :show-close="false"
      :destroy-on-close="true"
      :close-on-click-modal="false"
    >
      <gd_activeCode v-if="dialog.activeCodes" :templateID="nowTemplateID" />
      <el-button
        @click="
							dialog.activeCodes = false;
						"
      >
        关闭
      </el-button>
    </el-dialog>

    <!-- 开卡数据 -->
    <el-dialog
      title="激活卡信息"
      :visible.sync="dialog.overCreate"
      width="85%"
      :show-close="false"
      :destroy-on-close="true"
      :close-on-click-modal="false"
    >
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>{{overCreateData.name}}</span>
          <el-button style="float: right; padding: 10px 10px" type="success" @click="fzcards(overCreateData.arrs)">复制</el-button>
        </div>
        <div v-for="(item,index) in overCreateData.arrs" :key="index" class="text item">
          {{ item.activeCode }}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ item.activeCodeTypeName.split("-")[0] }}  ({{ item.activeDays}}天)
        </div>
      </el-card>
      <el-button
        @click="dialog.overCreate = false;overCreateData.name=null;overCreateData.arrs=[]">
        关闭
      </el-button>
    </el-dialog>

  </div>
</template>

<script>
import * as axiosAPI from '@/api/gpt/modetemplate.js';
import * as modesAPI from '@/api/gpt/modes.js';
import * as activeCodeAPI from '@/api/gpt/activeCode.js';
import * as paramAPI from '@/api/param.js';
import gd_param from "@/components/mycomponents/gd_param.vue";
import gd_file from "@/components/mycomponents/gd_file.vue";
import gd_activeCode from "@/components/gpt/activecode/index";
export default {
  name: 'modesTemplateIndex',
  components:{gd_file, gd_param,gd_activeCode},
  data() {
    return {
      //查询表单数据
      queryForm: {
        code: null,
        gptVipTypeCode:null
      },
      //表格数据
      tableData: [],
      //主表-分页数据
      pageData: {
        page: 1,
        size: 10,
        total: 0
      },
      //当前表格复选数据
      nowTableSelected: [],
      //弹窗状态
      dialog: {
        isEdit: false,
        saveOrEditDiv: false,
        showDiv: false,
        moreCreate:false,
        activeCodes:false,
        overCreate:false
      },
      //添加编辑表单-检验
      saveOrEditDataFormRules:{
        name: { required: true, message: '请输入模板名称', trigger: 'blur' },
        gptVipTypeCode:{ required: true, message: '请选择会员类型', trigger: 'change' },
      },
      saveOrEditData:{
        template:{
          templateID:null,
          name:null,
          code:null,
          isNewStaffTemplate:"0",
          gptVipTypeCode:null,
          gptVipTypeName:null
        },
        infos:[]
      },
      //展示表单数据
      showData: {
        template:{
          templateID:null,
          name:null,
          code:null,
          isNewStaffTemplate:"0",
          gptVipTypeCode:null,
          gptVipTypeName:null
        },
        infos:[]
      },
      //表单加载值
      formLoading: false,
      // 字典数据
      paramsData:{
        modeGrantUseType:[]
      },
      // 批量开卡表单
      moreCreateData:{
        templateID:null,
        templateName:null,
        activeCodeTypeCode:null,
        activeCodeTypeName:null,
        activeDays:0,
        createCount:1
      },
      moreCreateDataFormRules:{
        activeCodeTypeCode: { required: true, message: '请输入模板名称', trigger: 'change' },
        activeDays: { required: true, message: '请输入激活天数', trigger: 'blur' },
        createCount:{ required: true, message: '请输入开卡数量', trigger: 'blur' }
      },
      // 查看开卡情况
      nowTemplateID:null,
      // 开卡结束后信息
      overCreateData:{
        name:null,
        arrs:[]
      },
      // 现在的模型列表
      nowModelList:[],
      // 修改模型时当前要添加的模型
      editNowSelectModel:null
    };
  },
  mounted() {
    this.getTableDatas();
    this.getParamDatasModeGrantUseType();
    this.initModelListData()
  },
  filters:{
    timeFormat:function(value){
      let date = new Date(value);
      let year = date.getFullYear();
      let month = date.getMonth() + 1 +'';
      let day = date.getDate() +'';
      let hour = date.getHours() +'';
      let minute = date.getMinutes() +'';
      let second = date.getSeconds() +'';
      return `${year}-${month.padStart(2,'0')}-${day.padStart(2,'0')} ${hour.padStart(2,'0')}:${minute.padStart(2,'0')}:${second.padStart(2,'0')}`
    }
  },
  computed:{
    getTagType(){
      return(data) =>{
        if(data == "1"){
          return "info";
        }else if(data == "2"){
          return "ppp";
        }else if(data == "3"){
          return "warning";
        }else if(data == "4"){
          return "danger";
        }else{
          return "success";
        }
      }
    }
  },
  methods: {
    // 初始化模型数据
    initModelListData(){
      let _this = this;
      modesAPI.list()
        .then(function(res){
          if(res.code == "0000"){
            let tempList = res.data;
            var tempInfoList = [];
            for(var i in tempList){
              var temp = {
                modelsID:tempList[i].modelsID,
                modelsName:tempList[i].name,
                modeGrantUseTypeCode:"1",
                modeGrantUseTypeName:"不限量",
                isHaveDayLimit:tempList[i].isHaveDayLimit,
                dayLimitCount:tempList[i].dayLimitCount,
                mindex:tempList[i].mindex
              }

              if(tempList[i].modelTypeCode == 1){ // 不限量
                temp.modeGrantUseTypeCode = "1";
                temp.modeGrantUseTypeName = "不限量";
              }else if(tempList[i].modelTypeCode == 2){ // 普通限量
                temp.modeGrantUseTypeCode = "2";
                temp.modeGrantUseTypeName = "每日限量";
              }else if(tempList[i].modelTypeCode == 3){// 会员不限量
                temp.modeGrantUseTypeCode = "1";
                temp.modeGrantUseTypeName = "不限量";
              }else if(tempList[i].modelTypeCode == 4){// 会员每日限量
                temp.modeGrantUseTypeCode = "2";
                temp.modeGrantUseTypeName = "每日限量";
              }else if(tempList[i].modelTypeCode == 5){ // 即将开放
                temp.modeGrantUseTypeCode = "4";
                temp.modeGrantUseTypeName = "即将开放";
              }
              tempInfoList.push(temp);
            }
            _this.nowModelList = tempInfoList;
          }
        })
    },
    // 查询字典数据-模型授权使用类型
    getParamDatasModeGrantUseType(){
      let _this = this;
      paramAPI.getParamByCode("modeGrantUseType")
        .then(function(res){
          if(res.code == "0000"){
            _this.paramsData.modeGrantUseType = res.data;
          }
        })
    },
    //查询表格数据
    getTableDatas() {
      let _this = this;
      axiosAPI.pages(_this.queryForm, _this.pageData)
        .then(function(response) {
          console.log(response);
          if (response.code == '0000') {
            _this.tableData = response.data.rows;
            _this.pageData.total = response.data.total;
          }
        });
    },
    //重置查询条件表单
    resetQueryForm() {
      this.queryForm = {
        code: null,
        gptVipTypeCode:null
      };
      this.getTableDatas();
    },
    //重置添加、编辑、展示表单
    resetAddOrEditOrShowForm() {
      this.showData = {
        template:{
          templateID:null,
          name:null,
          code:null,
          isNewStaffTemplate:"0",
          gptVipTypeCode:null,
          gptVipTypeName:null
        },
        infos:[]
      };
      this.saveOrEditData = {
        template:{
          templateID:null,
          name:null,
          code:null,
          isNewStaffTemplate:"0",
          gptVipTypeCode:null,
          gptVipTypeName:null
        },
        infos:[]
      }
    },
    //提交-添加、编辑表单
    submitSaveOrEdit() {
      let _this = this;
      this.$refs.saveOrEditDataForm.validate(valid => {
        if (valid) {
          this.formLoading = true;
          let _this = this;

          // 更新授权选择的文字
          for(var c1 in this.saveOrEditData.infos){
            for(var c2 in this.paramsData.modeGrantUseType){
              if(this.saveOrEditData.infos[c1].modeGrantUseTypeCode == this.paramsData.modeGrantUseType[c2].dataValue){
                this.saveOrEditData.infos[c1].modeGrantUseTypeName = this.paramsData.modeGrantUseType[c2].dataName;
              }
            }
          }

          if(_this.dialog.isEdit == true){// 编辑

            axiosAPI.update(_this.saveOrEditData)
              .then(function(res) {
                if (res.code == '0000') {
                  _this.dialog.saveOrEditDiv = false;
                  _this.dialog.isEdit = false;
                  _this.resetAddOrEditOrShowForm();
                  _this.formLoading = false;
                  _this.getTableDatas();
                } else {
                  _this.formLoading = false;
                }
              });

          }else{ // 添加
            axiosAPI.add(_this.saveOrEditData)
              .then(function(res) {
                if (res.code == '0000') {
                  _this.dialog.saveOrEditDiv = false;
                  _this.dialog.isEdit = false;
                  _this.resetAddOrEditOrShowForm();
                  _this.formLoading = false;
                  _this.getTableDatas();
                } else {
                  _this.formLoading = false;
                }
              });
          }
        }
      });
    },
    //提交-删除
    submitDelete(ids){
      let _this = this;
      axiosAPI.deletes(ids)
        .then(res => {
          if (res.code == '0000') {
            _this.getTableDatas();
          }
        });
    },
    //分页条目数切换
    tableSizeChange(size) {
      this.pageData.size = size;
      this.getTableDatas();
    },
    //页码切换
    tableCurrentChange(page) {
      this.pageData.page = page;
      this.getTableDatas();
    },
    //行-查看
    rowShow(data) {
      var _this = this;
      axiosAPI.getByID(data.templateID)
        .then(function(res){
          if(res.code == "0000"){
            _this.showData = res.data;
            _this.dialog.showDiv = true;
          }
        });
    },
    //行-编辑
    rowEdit(data) {

      let _this = this;
      axiosAPI.getByID(data.templateID)
        .then(function(res){
          if(res.code == "0000"){
            _this.saveOrEditData = res.data;
            _this.dialog.isEdit = true;
            _this.dialog.saveOrEditDiv = true;
          }
        });
    },
    //行-删除
    rowDelete(data) {
      this.$confirm('确定要删除这行数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.submitDelete(data.templateID);
      });
    },
    //点击批量删除
    moreDeletes() {
      if(this.nowTableSelected.length<=0){
        this.$notify.warning({
          title: '删除提示',
          message: '请至少选择一条数据再删除'
        });
        return;
      }
      let ids = '';

      for (var i in this.nowTableSelected) {
        ids += this.nowTableSelected[i].templateID + ',';
      }

      ids = ids.substring(0, ids.length - 1);

      //调用删除
      this.submitDelete(ids);
    },
    //表格批量选择
    tableDataSelected(data){
      this.nowTableSelected = data;
    },
    // 查询ai模板数据
    getAIModesList(){
      let _this = this;
      modesAPI.list()
        .then(function(res){
           if(res.code == "0000"){
              let tempList = res.data;
              var tempInfoList = [];
              for(var i in tempList){
                var temp = {
                  modelsID:tempList[i].modelsID,
                  modelsName:tempList[i].name,
                  modeGrantUseTypeCode:"1",
                  modeGrantUseTypeName:"不限量",
                  isHaveDayLimit:tempList[i].isHaveDayLimit,
                  dayLimitCount:tempList[i].dayLimitCount,
                  mindex:tempList[i].mindex
                }

                if(tempList[i].modelTypeCode == 1){ // 不限量
                  temp.modeGrantUseTypeCode = "1";
                  temp.modeGrantUseTypeName = "不限量";
                }else if(tempList[i].modelTypeCode == 2){ // 普通限量
                  temp.modeGrantUseTypeCode = "2";
                  temp.modeGrantUseTypeName = "每日限量";
                }else if(tempList[i].modelTypeCode == 3){// 会员不限量
                  temp.modeGrantUseTypeCode = "1";
                  temp.modeGrantUseTypeName = "不限量";
                }else if(tempList[i].modelTypeCode == 4){// 会员每日限量
                  temp.modeGrantUseTypeCode = "2";
                  temp.modeGrantUseTypeName = "每日限量";
                }else if(tempList[i].modelTypeCode == 5){ // 即将开放
                  temp.modeGrantUseTypeCode = "4";
                  temp.modeGrantUseTypeName = "即将开放";
                }
                tempInfoList.push(temp);
              }
             _this.saveOrEditData.infos = tempInfoList;
           }
        })
    },
    // 模型列表中-行删除
    rowInfoDelete(data){
      this.$confirm('确定要删除这行数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let index = 0;
        for(var i in this.saveOrEditData.infos){
          if(this.saveOrEditData.infos[i].modelsID == data.modelsID){
            index = i;
            break;
          }
        }
        this.saveOrEditData.infos.splice(index,1);
      });

    },
    // 设置新人默认模板
    isNewStaffTemplateChange(data,row){
      if(data == 0){
        row.isNewStaffTemplate = "1";
        this.$notify.warning({
          title: '操作提示',
          message: '已设置为默认新人注册模板时不可关闭设置，设置其他模板即可关闭'
        });
        return;
      }
      let _this = this;
      axiosAPI.setDefaultNewStaffTemplate(row.templateID)
        .then(function(res){
          _this.getTableDatas();
        });
    },
    // 批量开卡
    rowMoreCreate(data){
      this.moreCreateData.templateID = data.templateID;
      this.moreCreateData.templateName = data.name;
      this.dialog.moreCreate = true;
    },
    // 激活卡类型切换
    activeCodeTypeChange(data){
      if(data == undefined){
        this.moreCreateData.activeCodeTypeCode = null;
        this.moreCreateData.activeCodeTypeName = null;
        this.moreCreateData.activeDays = 0;
      }else{
        this.moreCreateData.activeCodeTypeCode = data.dataValue;
        this.moreCreateData.activeCodeTypeName = data.dataName;
        if(data.dataValue == 2){ // 月卡
          this.moreCreateData.activeDays = 31;
        }else if(data.dataValue == 3){ // 年卡
          this.moreCreateData.activeDays = 365;
        }else if(data.dataValue == 4){ // 终身
          this.moreCreateData.activeDays = 9999;
        }
      }
    },
    // 提交批量开卡
    submitMoreCreate(){
      let _this = this;
      this.$refs.moreCreateDataForm.validate(valid => {
        if (valid) {
          _this.formLoading = true;
          activeCodeAPI.createAcitveCodes(_this.moreCreateData)
            .then(function(res){
              if(res.code == "0000"){
                _this.formLoading = false;
                _this.closeMoreCreate();

              }else{
                _this.formLoading = false;
              }
            });
        }
      });
    },
    // 关闭批量开卡
    closeMoreCreate(){
      this.dialog.moreCreate = false;
      this.moreCreateData = {
        templateID:null,
        templateName:null,
        activeCodeTypeCode:null,
        activeCodeTypeName:null,
        activeDays:0,
        createCount:1
      };
    },
    // 查看所属模板激活卡
    rowShowActiveCode(data) {
      this.nowTemplateID = data.templateID;
      this.dialog.activeCodes = true;
    },
    // 会员类型切换
    gptVipTypeChange(data){
      if(data == undefined){
        this.saveOrEditData.template.gptVipTypeCode = null;
        this.saveOrEditData.template.gptVipTypeName = null;
      }else{
        this.saveOrEditData.template.gptVipTypeCode = data.dataValue;
        this.saveOrEditData.template.gptVipTypeName = data.dataName;
      }
    },
    // 授权信息-行编辑修改-是否有日上限选择
    rowIsHaveDayLimitChange(data){
      if(data.isHaveDayLimit==1){// 有日上限
        data.dayLimitCount = 0;
        data.useLess = 0;
      }else{ // 无上限
        data.dayLimitCount = -1;
        data.useLess = -1;
      }
    },
    // 进行复制
    fzcards(data){
      var datas = "";
      for(var i in data){

        datas += data[i].activeCode+"  "+data[i].activeCodeTypeName.split("-")[0] +"   ("+data[i].activeDays+"天)    \n"
      }
      this.ToClipboard(datas);
    },
    // 将数据赋值到剪贴板
    ToClipboard(text) {
      var textarea = document.createElement('textarea');
      textarea.value = text;
      document.body.appendChild(textarea);
      textarea.select();
      document.execCommand('copy');
      document.body.removeChild(textarea);
      this.$message.success("复制成功！")
    },
    // 查看可用卡
    rowShowActiveCodeCanUse(data){
      console.dir(data);
      let _this = this;
      activeCodeAPI.list({"isActive":"0","templateID":data.templateID})
        .then(function(res){
           if(res.code == "0000"){
             _this.overCreateData.name = data.name;
             _this.overCreateData.arrs = res.data;
             _this.dialog.overCreate = true;
           }
        })
    },
    // 导出可用卡
    rowShowActiveCodeCanUseEx(data){
      let _this = this;
      activeCodeAPI.listExport({"isActive":"0","templateID":data.templateID})
        .then(res => {
          console.log(res); // 控制台输出：Blob {size: 30208, type: 'application/x-msdownload'}
          var debug = res;
          if (debug) {
            var elink = document.createElement('a');
            elink.download = '导出表格.xls';
            elink.style.display = 'none';
            var blob = new Blob([debug], { type: 'application/x-msdownload' });
            elink.href = URL.createObjectURL(blob);
            document.body.appendChild(elink);
            elink.click();
            document.body.removeChild(elink);
          } else {
            _this.$message.error('导出异常请联系管理员');
          }
        }).catch(err => {
        console.log(err);
      });
    },
    // 修改时添加模型选中切花
    addModelForEdit(){
      if(this.editNowSelectModel==null){
        this.$message.error("请选择模型后再添加！");
       return;
      }
      var isHave = false;
      for(var i in this.saveOrEditData.infos){
         if(this.editNowSelectModel == this.saveOrEditData.infos[i].modelsID){
           isHave = true;
           break;
         }
      }
      if(isHave){
        this.$message.error("当前模型已添加，不要重复添加！");
      }else{
        // 找到数据添加
        var tempData = null;
        for(var i in this.nowModelList){
          if(this.editNowSelectModel == this.nowModelList[i].modelsID){
            tempData = this.nowModelList[i];
            break;
          }
        }
        if(tempData == null){
          this.$message.error("无匹配模型，无法添加！");
          return;
        }
        this.saveOrEditData.infos.push(tempData);
      }
    }
  }
};
</script>

<style scoped></style>
